import { axios } from '@/utils/request'
const isProd = process.env.NODE_ENV === 'production'
console.log('环境：', process.env.NODE_ENV)
const proUrl = '/api'
const devUrl = '/devtest'
const api = {
  user: '/user',
  role: '/role',
  service: '/service',
  permission: '/permission',
  permissionNoPager: '/permission/no-pager',
  orgTree: '/org/tree',
  test: isProd ? proUrl : devUrl,
  // test: isProd ? devUrl : proUrl,
  ruleBaseURL: '/basic-business/sys/fillRule/executeRuleByCodeMore/',
}
export { api }

// put、post去除 更某些字段
function removeCreate (data) {
  console.log('data ↓ ')
  console.log(data)
  const da = data
  const dl = Object.keys(data)
  if (dl.length > 0) {
    delete da.createBy
    delete da.createTime
    delete da.updateBy
    delete da.updateTime
  }
  return da
}
export function getUserList (parameter) {
  return axios({
    url: api.user,
    method: 'get',
    params: parameter
  })
}

export function getRoleList (parameter) {
  return axios({
    url: api.role,
    method: 'get',
    params: parameter
  })
}

export function getServiceList (parameter) {
  return axios({
    url: api.service,
    method: 'get',
    params: parameter
  })
}

export function getPermissions (parameter) {
  return axios({
    url: api.permissionNoPager,
    method: 'get',
    params: parameter
  })
}

export function getOrgTree (parameter) {
  return axios({
    url: api.orgTree,
    method: 'get',
    params: parameter
  })
}

// id == 0 add     post
// id != 0 update  put
export function saveService (parameter) {
  return axios({
    url: api.service,
    method: parameter.id === 0 ? 'post' : 'put',
    data: parameter
  })
}
export function userList (parameter) {
  return axios({
    url: '/api/permission/secStaff/list',
    method: 'get',
    params: parameter
  })
}

// post
export function postAction (url, parameter) {
  return axios({
    url: api.test + url,
    method: 'post',
    data: removeCreate(parameter)
  })
}

// post method= {post | put}
export function httpAction (url, parameter, method) {
  return axios({
    url: api.test + url,
    method: method,
    data: removeCreate(parameter)
  })
}

// put
export function putAction (url, parameter) {
  return axios({
    url: api.test + url,
    method: 'put',
    data: removeCreate(parameter)
  })
}

// get
export function getAction (url, parameter) {
  return axios({
    url: api.test + url,
    method: 'get',
    params: parameter
  })
}

// deleteAction
export function deleteAction (url, parameter, data) {
  return axios({
    url: api.test + url,
    method: 'delete',
    params: parameter, 
    data
  })
}

// 基础数据使用方法 ↓
// post
export function postActionBasic (url, parameter) {
  return axios({
    url: proUrl + url,
    method: 'post',
    data: removeCreate(parameter)
  })
}
// put
export function putActionBasic (url, parameter) {
  return axios({
    url: proUrl + url,
    method: 'put',
    data: removeCreate(parameter)
  })
}
// get
export function getActionBasic (url, parameter) {
  return axios({
    url: proUrl + url,
    method: 'get',
    params: parameter
  })
}
// deleteAction
export function deleteActionBasic (url, parameter) {
  return axios({
    url: proUrl + url,
    method: 'delete',
    params: parameter
  })
}
// 基础数据使用方法 结束 ↑

/**
 * 下载文件 用于excel导出
 * @param url
 * @param parameter
 * @returns {*}
 */
export function downFile (url, parameter) {
  return axios({
    url: api.test + url,
    params: parameter,
    method: 'get',
    responseType: 'blob'
  })
}

/**
 * 获取文件访问路径
 * @param avatar
 * @param imgerver
 * @param str
 * @returns {*}
 */
export function getFileAccessHttpUrl (avatar, imgerver, subStr) {
  if (avatar && avatar.indexOf(subStr) != -1) {
    return avatar
  } else {
    return imgerver + '/' + avatar
  }
}

export function groupListChild (parameter) { // 获取子组织
  return axios({
    url: '/api/permission/secOrg/bizList',
    method: 'get',
    params: parameter
  })
}

export function groupList (parameter) { // 获取一级组织
  return axios({
    url: '/api/permission/secOrg/list',
    method: 'get',
    params: parameter
  })
}
